(function () {

	'use strict';

	angular
		.module('app')
		.controller('CategoryController', CategoryController);

	CategoryController.$inject = ['$scope', '$http', '$rootScope', '$timeout', '$location', '$state', 'API', 'ENUM', 'CONSTANTS', '$window', 'AppAuthenticationService', 'CartModel', 'ConfigModel'];

	function CategoryController($scope, $http, $rootScope, $timeout, $location, $state, API, ENUM, CONSTANTS, $window, AppAuthenticationService, CartModel, ConfigModel) {

		var PER_PAGE = 1000;
		var MAX_CATEGORIES = 4;
        var MAX_PRODUCTS = 4;

        var emptyProduct = {};
        var emptyProducts = [];

        for (var i = 0; i < MAX_PRODUCTS; ++i) {
            emptyProducts.push(emptyProduct);
        }

		$scope.categories = [];
		$scope.selectedSide = null;
		$scope.newArrival = emptyProducts;
		$scope.touchProduct = _touchProduct;
		$scope.formatUrl = _formatUrl;


		$scope.touchSearch = _touchSearch;
		$scope.touchSide = _touchSide;
		$scope.touchMain = _touchMain;

		$scope.cartModel = CartModel;

		function _touchSearch() {
			$state.go('search', {});
		}

		function _touchSide(side) {
			$scope.selectedSide = side;
			$scope.subCategories = side.categories;
		}

		function _touchMain(main) {
			if (!main) {

				var side = $scope.selectedSide;

				$state.go('search-result', {
					sortKey: ENUM.SORT_KEY.DEFAULT,
					sortValue: ENUM.SORT_VALUE.DEFAULT,
					keyword: null,
					category: side.id,
					navTitle: side.name,
					title:    side.name,
					navStyle: 'default'
				});

			} else {

				$state.go('search-result', {
					sortKey: ENUM.SORT_KEY.DEFAULT,
					sortValue: ENUM.SORT_VALUE.DEFAULT,

					keyword: null,
					category: main.id,
					title:    main.name,
					navTitle: main.name,
					navStyle: 'default'
				});

			}
		}

		function _reloadCategories() {
			API.category
				.list({
					page: 1,
					per_page: MAX_CATEGORIES
				})
				.then(function (categories) {
					if (categories && categories.length) {
						$scope.categories = categories;
						$scope.selectedSide = categories[0];
						$scope.subCategories = categories[0].categories;
					} else {
						$scope.categories = null;
						$scope.selectedSide = null;
						$scope.subCategories = null;
					}
				});
		}

		// begin
			1
			function _touchCategory(category) {
            $state.go('search-result', {
                sortKey: ENUM.SORT_KEY.DEFAULT,
                sortValue: ENUM.SORT_VALUE.DEFAULT,

                keyword: null,
                category: category.id,

                navTitle: category.name,
                navStyle: 'default'
            });

        	}

			function _touchProduct(product) {
        	// var url = $location.protocol() + '://' + $location.host() + ':' + $location.port() + '/#/product?product=' + product.id;
        	// return url;
        	// window.location.href = url;
            $state.go('product', {
                product: product.id,
            });
        	}

        	function _formatUrl(url) {
            var timestamp = Math.round(new Date().getTime() / 1000);

            if (-1 == url.indexOf('?')) {

                return url + '?v=' + timestamp;
            } else {
                return url + '&v=' + timestamp;
            }
        }

			function _reloadNewArrival() {
            API.product
                .list({
                    page: 1,
                    per_page: MAX_PRODUCTS,
                    sort_key: ENUM.SORT_KEY.DATE,
                    sort_value: ENUM.SORT_VALUE.DESC
                })
                .then(function(products) {
                    $scope.newArrival = products;
                });
      		  }

			

		// end

		function _reload() {
			_reloadCategories();
			_reloadNewArrival();
		}

		_reload();
	}

})();